//
//  ZhuanTViewController.m
//  LT
//
//  Created by dllo on 16/5/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ZhuanTViewController.h"
#import "ZhuanTTableViewCell.h"
#import "ZhuanTModel.h"
#import "ZHNetworking.h"
#import "ZhuanTDetailViewController.h"

static NSString *const zhuanTCell = @"zhuanTCell";
@interface ZhuanTViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *zhuanTTableView;

//做一个装PHOTO的数组
@property(nonatomic,strong)NSMutableArray *photoArray;


@end

@implementation ZhuanTViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.photoArray = [NSMutableArray array];
    [self getData];
    
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.zhuanTTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    //[self getData];
    self.zhuanTTableView.backgroundColor = [UIColor whiteColor];
    self.zhuanTTableView.delegate = self;
    self.zhuanTTableView.dataSource = self;
    self.zhuanTTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.zhuanTTableView];
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController setNavigationBarHidden:NO];
    self.navigationItem.hidesBackButton = YES;
    
   self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"goback"] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction)];
    
    //注册
    [self.zhuanTTableView registerClass:[ZhuanTTableViewCell class] forCellReuseIdentifier:@"zhuanTCell"];
   }

-(void)leftButtonAction{
    [self.navigationController dismissViewControllerAnimated:YES  completion:^{
        
    }];
    
}

//cell点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ZhuanTDetailViewController *zhuanTDetailVC = [[ZhuanTDetailViewController alloc]init];
    ZhuanTModel *model = [self.photoArray objectAtIndex:indexPath.row];
    
    zhuanTDetailVC.model = model;
    
    UINavigationController *zhuanTDetailNa = [[UINavigationController alloc]initWithRootViewController:zhuanTDetailVC];
    
    [self presentViewController:zhuanTDetailNa animated:YES completion:^{
        
        
    }];
    //专题的model就是photoArray数组中的每一个对应小字典
    ZhuanTModel *zhuanTModel = [self.photoArray objectAtIndex:indexPath.row];
    //标题title就是model的title
    zhuanTDetailVC.navigationItem.title = zhuanTModel.title;
    
    zhuanTDetailVC.ZhuanTString = zhuanTModel.url;

}


//俩个必备方法
//cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
    
}

//cell数量
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    

    return self.photoArray.count;

}

//cell的执行方法
-(ZhuanTTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
  ZhuanTTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"zhuanTCell"];
    
    ZhuanTModel *model = [self.photoArray objectAtIndex:indexPath.row];
    
    cell.cellModel = model;
    
    return cell;
    
}

//"查看精彩专题"的数据请求

-(void)getData{

    [ZHNetworking zhGETWithURL:@"http://open.qyer.com/qyer/special/topic/special_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&page=1&track_app_channel=App%2520Store&track_app_version=6.8.5&track_device_info=iPhone%25205s&track_deviceid=E3EBA485-00FA-421D-BACB-D791AF3400E2&track_os=ios%25209.3.1&v=1" parameter:nil succes:^(id result) {
       //之前上面没有初始化数组导致这里崩溃了
        NSArray *dataArray = [result objectForKey:@"data"];
        
        for (NSDictionary *dic in dataArray) {
            ZhuanTModel *model = [[ZhuanTModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.photoArray addObject:model];
        }

        [self.zhuanTTableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];

  
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
